Attribute VB_Name = "mModule"
Option Explicit


'============ our database to track CIRC and Node info
Public gBase() As String, gbLock() As Boolean
Public gEntry(2, 9) As String, gLock(9) As Boolean
'gBase  = FP|GeoLoc /gEntry = FP|GeoLoc|gbID
'// gLock serves as a BlackList because
'// there is no reason for us to change gEntry unless it is unusable

Public nBase() As String, nLock() As Boolean
'Public nBanned() As Boolean
'nBase = FP|GeoLoc

'cBase = 0-CircID|1-Hop1/gID|2-Hop2/gbID|3-Hop3/nID|4-currentLength|5- desired circ length

'for currnetLength
'-2  closed or failed, no action is taken
'-1  command sent waiting for circuitid
'0   circid assigned
'1   1st hop OK
'2   2nd hop OK
'3   3rd hop OK

Public cBase(5, 5) As Integer, cBench(1, 5) ',cLock() As Boolean
Public c2HopMode(5) As Boolean
Public cUpTime(5) As Long
Public cStreamCount(5) As Integer
'cSec = 0-MaxTimeVal|1-MaxCountVal
Public cSec_2Hop(1) As Integer
Public cSec_3Hop(1) As Integer
'Public sBase(2, 5) As Long
'CID|gID|nID
Public last_Failed_cID As Integer 'steam deteched from this circ



'=========== for events and replys
Public eQueue() As String, rQueue() As String, cQueue() As String
Public cQLock As Boolean
Public evQ() As String, ev As Integer
Public oBuffer As String, jBuffer As String




'============ for stats
Public STAT_CIRC As Integer
Public STAT_FAIL_STREAM As Integer
Public STAT_eQ As Integer
Public STAT_rQ As Long
Public STAT_BW(1) As Long
Public STAT_FAIL_Relay As Integer
'BW = 0-upload|1-download



Public LiteMode As Boolean 'switch to 2-hop CIRC


'SendCmd
Public ForceExit As Boolean    'in cmdsend & form_unload
Public oInPro As Boolean
Public lastCmd As String



Public runPath As String



Public Function SendCmd(ByVal cmdStr As String, Optional Wait As Boolean = True, Optional Retry As Boolean = True) As Boolean
Dim i As Long, j As Integer
        If ForceExit Then Exit Function
'Do While oInPro
'    DoEvents
'    If i = 500 Then
 '       logMan "SendCollison", cmdStr + "|last=" + lastCmd + vbCrLf _
'                + "SocksState=" + CStr(o.State)
'        j = j + 1
'        If j > 2 Then
'            logMan "QuitOnSendCollison", "x3"
'            Unload Me
''            End
'        End If
'    End If
'    i = i + 1
'Loop
    
    ReDim rQueue(0)
    oInPro = True
    
    If TorCtrl.o.State <> 7 Then
        SendCmd = False
        logMan "SendCmd~False", "not o free"
        Exit Function
    End If
    
    TorCtrl.o.SendData (cmdStr & vbCrLf)
    logMan "Send", cmdStr

j = 0


If Wait Then
    Do While UBound(rQueue) = 0 And rQueue(0) = ""
        If ForceExit Then Exit Function
        DoEvents
        If i = 2000 Then
            If Retry Then
                TorCtrl.o.SendData cmdStr & vbCrLf: i = 0
                logMan "reSend=", cmdStr + vbCrLf _
                    + "LastCmd=" + lastCmd + vbCrLf _
                    + "rQueueSize=" + Str(UBound(rQueue)) + vbCrLf _
                    + "eQueueSize=" + Str(UBound(eQueue)) + "/" + Str(UBound(evQ)) + vbCrLf _
                    + "oBuffer=" + oBuffer + vbCrLf _
                    + "jBuffer=" + jBuffer + vbCrLf _
                    + "SocksState=" + CStr(TorCtrl.o.State)
                    j = j + 1
                    If j > 2 Then
                        logMan "QuitOnReSend", "x3"
                        Unload TorCtrl
                        End
                    End If
            End If
        End If
        i = i + 1
    Loop
End If
oInPro = False
lastCmd = cmdStr
SendCmd = True
End Function












